package com.applink.sell_proof.fragment;

import java.util.List;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.applink.sell_proof.BasePagerActivity;
import com.applink.sell_proof.CustomerServiceActivity;
import com.applink.sell_proof.R;
import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.database.table.CountryDBHelper;
import com.applink.sell_proof.database.table.CustomerSupportHelper;
import com.applink.sell_proof.model.Country;
import com.applink.sell_proof.model.UserProfile;

public class EditUserProfile extends DialogFragment implements OnClickListener {

	public static final String	TAG	= "EditUserProfile";
	private UserProfile	mUserProfile;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_TITLE, 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.edit_user_profile, null, false);
		mUserProfile = CustomerSupportHelper.GetUserProfile();
		((TextView) view.findViewById(R.id.txtOragnization)).setText(mUserProfile.getOrangeName());
		((TextView) view.findViewById(R.id.edtDesignation)).setText(mUserProfile.getDesignation());
		((TextView) view.findViewById(R.id.txtSalValue)).setText(mUserProfile.getSalutation());
		((TextView) view.findViewById(R.id.txtfirstname)).setText(mUserProfile.getFirstname());
		((TextView) view.findViewById(R.id.txtlastname)).setText(mUserProfile.getLastname());
		((TextView) view.findViewById(R.id.edt_userpro_mobile)).setText(mUserProfile.getMobileNo());
		((TextView) view.findViewById(R.id.edtEmailContact)).setText(mUserProfile.getEmail());
		view.findViewById(R.id.btn_save).setOnClickListener(this);
		view.findViewById(R.id.btn_close).setOnClickListener(this);

		// initial country
		List<Country> countries = CountryDBHelper.getAllCountry();
		if (countries != null) {
			// add dummy data to show select
			Country country = new Country();
			country.setCode(-1);
			country.setName(getString(R.string.spinner_header));
			countries.add(0, country);

			ArrayAdapter<Country> adapter_country = new ArrayAdapter<Country>(getActivity(), R.layout.spinner_item, countries);
			((Spinner) view.findViewById(R.id.txtCountryVal)).setAdapter(adapter_country);
			for (int i = 0; i < countries.size(); i++) {
				if (mUserProfile.getCountryCode() == countries.get(i).getCode()) {
					((Spinner) view.findViewById(R.id.txtCountryVal)).setSelection(i);
					break;
				}
			}
		}
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_close:
			dismiss();
			break;
		case R.id.btn_save:
			if (checkValidate()) {
				String Designation = (((TextView) getView().findViewById(R.id.edtDesignation)).getText().toString());
				String MobileNo = (((TextView) getView().findViewById(R.id.edt_userpro_mobile)).getText().toString());
				String Email = (((TextView) getView().findViewById(R.id.edtEmailContact)).getText().toString());
				Spinner spinner = ((Spinner) getView().findViewById(R.id.txtCountryVal));
				Country country = (Country) spinner.getSelectedItem();
				if (country.getCode() != -1) {
					mUserProfile.setCountryCode(country.getCode());
					mUserProfile.setCountryName(country.getName());
				}
				mUserProfile.setDesignation(Designation);
				mUserProfile.setMobileNo(MobileNo);
				mUserProfile.setEmail(Email);
				CustomerSupportHelper.InsertUserPorfileToSharePref(mUserProfile);
				DataHelper.getInstance().updateUserProfile(mUserProfile);
				((CustomerServiceActivity) getActivity()).updateUserProfile();
				dismiss();
				DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case Dialog.BUTTON_NEGATIVE:
							dialog.dismiss();
							break;
						default:
							break;
						}
					}
				};
				((BasePagerActivity)getActivity()).showAlertDialog(listener, "User Profile", "Close", null, getString(R.string.msg_update_profile_successful), true);
			}
			break;
		default:
			break;
		}
	}
	
	private boolean checkValidate() {
		clearError();
		String mailRegex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		boolean validate = true;
		String designation = (((TextView) getView().findViewById(R.id.edtDesignation)).getText().toString());
		if(designation == null || designation.trim().length() == 0) {
			validate = false;
			getView().findViewById(R.id.designation_error).setVisibility(View.VISIBLE);
		}
		String mobile = (((TextView) getView().findViewById(R.id.edt_userpro_mobile)).getText().toString());
		if(mobile == null || mobile.trim().length() == 0) {
			validate = false;
			getView().findViewById(R.id.phone_error).setVisibility(View.VISIBLE);
		}
		String email = (((TextView) getView().findViewById(R.id.edtEmailContact)).getText().toString());
		if(email == null || email.trim().length() == 0) {
			validate = false;
			getView().findViewById(R.id.email_error).setVisibility(View.VISIBLE);
		}
		if( email.matches(mailRegex) == false) {
			((TextView)getView().findViewById(R.id.email_error)).setText(getString(R.string.error_email_invalid));
			getView().findViewById(R.id.email_error).setVisibility(View.VISIBLE);
			validate = false;
		}
		
		Spinner spinner = ((Spinner) getView().findViewById(R.id.txtCountryVal));
		Country country = (Country) spinner.getSelectedItem();
		if (country.getCode() == -1) {
			((TextView)getView().findViewById(R.id.country_error)).setText(getString(R.string.error_select_country));
			getView().findViewById(R.id.country_error).setVisibility(View.VISIBLE);
			validate = false;
		}
		return validate;
	}
	
	private void clearError(){
		getView().findViewById(R.id.designation_error).setVisibility(View.GONE);
		getView().findViewById(R.id.phone_error).setVisibility(View.GONE);
		getView().findViewById(R.id.email_error).setVisibility(View.GONE);
	}

}
